var app = getApp();
var imgPath;
var bgPath = 'https://www.the-bestme.com/public/logo.jpg';
var buy_bgPath = 'https://www.the-bestme.com/public/buyqrbg.jpg'
var buyqr, addqr;
var qrtype;
var window_wtd,window_hgt;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    btnDesc: '保存图片到相册'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(!options.qrtype){
      wx.navigateBack({});
      return;
    }
    qrtype = options.qrtype;
    if (qrtype == 1){
      buyqr = options.buyqr;
      wx.setNavigationBarTitle({
        title: '分享',
      });
    }
    else if (qrtype == 2){
      addqr = options.addqr;
      wx.setNavigationBarTitle({
        title: '推广',
      });
    }
    wx.getSystemInfo({
      success: function(res) {
        window_wtd = res.windowWidth - 30 - 30;
        window_hgt = res.windowHeight -20 -20 -55;
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.createPoster();
    console.log(qrtype)
    if(qrtype == 1){
      if(buyqr){
        imgPath = app.globalData.webUrl + buyqr;
        this.createPoster(buy_bgPath);
      }
      else{
        app.showLoading();
        var url = '/index.php/api/User/createUserQr'
        var params = {
          duid: 0,
          tp: 'buy'
        };
        app.request(url, params).then(res => {
          app.hideLoading();
          if(res.status == 'ok'){
            buyqr = res.result;
            imgPath = app.globalData.webUrl + buyqr;
            this.createPoster(buy_bgPath);
          }
          else{
            app.toast.error(res.msg).then(()=>{
              wx.navigateBack({});
            });
          }
        });    
      }
    }
    else if(qrtype == 2){
      if (addqr) {
        imgPath = app.globalData.webUrl + addqr;
        this.createPoster(bgPath);
      }
      else{
        app.showLoading();
        var url = '/index.php/api/User/createUserQr'
        var params = {
          duid: app.globalData.duserID,
          tp: 'add'
        };
        app.request(url, params).then(res => {
          app.hideLoading();
          if (res.status == 'ok') {
            addqr = res.result;
            imgPath = app.globalData.webUrl + addqr;
            this.createPoster(bgPath);
          }
          else{
            app.toast.error(res.msg).then(() => {
              wx.navigateBack({});
            });
          }
        });  
      }
    }
  },
  createPoster:function(imgBgPath){
    app.showLoading();
    Promise.all([
      app.requestImage(imgBgPath),
      app.requestImage(imgPath)
    ]).then(res => {
      const ctx = wx.createCanvasContext('shareCanvas')

      // 底图
      ctx.drawImage(res[0].path, 0, 0, window_wtd, window_hgt)
      
      const qrImgSize = 200 *  window_wtd/ 600;
      const offsetY = window_wtd + (window_hgt - window_wtd - qrImgSize) * 0.5;
      const x1Poz = (window_wtd - qrImgSize) / 2;
      // const x1Poz = (window_wtd - qrImgSize) / 2 - qrImgSize * 0.5 + 10;
      const x2Poz = x1Poz + qrImgSize * 0.5;
      const y1Poz = offsetY + qrImgSize * 0.5;
      const headiconSize = 60;
      const x3Poz = window_wtd * 0.5 + headiconSize * 0.5 + 10;

      ctx.arc(x2Poz, y1Poz, qrImgSize * 0.55, 0, Math.PI * 2, false);
      ctx.setFillStyle('#ffffff');
      ctx.fill();
      ctx.drawImage(res[1].path, x1Poz, offsetY, qrImgSize, qrImgSize);

      // ctx.setTextAlign('center')    
      // ctx.setFillStyle('#000000')  
      // ctx.setFontSize(14)         
      // ctx.fillText(app.globalData.nickname, x3Poz+30, y1Poz + 45 , 60);

      // ctx.save();
      // const y2Poz = y1Poz - 40;
      // ctx.beginPath();
      // ctx.arc(x3Poz+30, y2Poz+30, 30, 0, Math.PI * 2, false);
      // ctx.clip();
      // ctx.drawImage(res[2].path,x3Poz,y2Poz,60,60);
      // ctx.restore();
      ctx.draw();

      app.hideLoading();
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareToIndex();
  },
  savePicture: function () {
    let that = this;
    wx.canvasToTempFilePath({
      canvasId: 'shareCanvas',
      success: (res) => {
        console.log(res);
        if (res.errMsg == "canvasToTempFilePath:ok"){
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success: (res) => {
              that.setData({
                btnDesc:'保存成功'
              });
              that.backToPrevPage();
            }
          });
        }
      }
    }, this);
  },
  backToPrevPage:function(){
    var delayId = setTimeout(function(){
      clearTimeout(delayId);
      wx.navigateBack({});
    },2000);
  }
})